package pers.hl.module_main.mine

import pers.hl.common.base.BaseViewModel
import pers.hl.common.base.SingleLiveEvent
import pers.hl.lib_global.common.MMKVHelper
import pers.hl.lib_global.model.bean.*
import java.io.File

class MineViewModel : BaseViewModel<MineModel>() {

    fun borrowBook(bookId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.borrowBook(bookId)
            result.msg
        }
    }

    fun fetchBookById(bookId: Int):SingleLiveEvent<Book> {
        return createSimpleEvent {
            val result = mModel.fetchBookById(bookId)
            result.data
        }
    }

    fun fetchMyBorrows(): SingleLiveEvent<List<BookBorRec>> {
        return createSimpleEvent {
            val result = mModel.fetchMyBorrows()
            result.data
        }

    }

    fun cancelBorrow(recordId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.cancel(recordId)
            result.msg
        }
    }

    fun fetchSharing(userId: Int): SingleLiveEvent<List<BookShareRec>> {
        return createSimpleEvent {
            val result = mModel.fetchSharing(userId)
            result.data
        }
    }

    fun fetchMyPublish(): SingleLiveEvent<List<BookShareRec>> {
        return createSimpleEvent {
            val result = mModel.fetchMyPublish()
            result.data
        }
    }

    fun collect(bookId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.collect(bookId)
            result.msg
        }
    }

    fun unCollect(bookId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.unCollect(bookId)
            result.msg
        }
    }

    fun fetchMyCollectBook(): SingleLiveEvent<List<Book>> {
        return createSimpleEvent {
            val result = mModel.fetchMyCollectBook()
            result.data
        }
    }

    fun deleteComment(cmtId: Int) : SingleLiveEvent<String>{
        return createSimpleEvent {
            val result = mModel.deleteComment(cmtId)
            result.msg
        }
    }

    fun fetchReplyList(cmtId: Int): SingleLiveEvent<List<Reply>> {
        return createSimpleEvent {
            val result = mModel.fetchReplyList(cmtId)
            result.data
        }
    }

    fun fetchMyComment(): SingleLiveEvent<List<Comment>> {
        return createSimpleEvent {
            val result = mModel.fetchMyComments(MMKVHelper.getUser()!!.userId)
            result.data
        }
    }

    fun fetchMyCollect(): SingleLiveEvent<List<Collect>> {
        return createSimpleEvent {
            val result = mModel.fetchMyCollect(MMKVHelper.getUser()!!.userId)
            result.data
        }
    }

    fun fetchBookByTag(tagId: Int): SingleLiveEvent<List<Book>> {
        return createSimpleEvent {
            val result = mModel.fetchBookByTag(tagId)
            result.data
        }
    }

    fun accept(bbrId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.accept(bbrId)
            result.msg
        }
    }

    fun refuse(bbrId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.refuse(bbrId)
            result.msg
        }
    }

    fun returnBack(bbrId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.returnBack(bbrId)
            result.msg
        }
    }

    fun fetchMyFollows() : SingleLiveEvent<List<User>>{
        return createSimpleEvent {
            val result = mModel.fetchMyFollows()
            result.data
        }
    }

    fun fetchMyFans() : SingleLiveEvent<List<User>>{
        return createSimpleEvent {
            val result = mModel.fetchMyFans()
            result.data
        }
    }

    fun fetchUserInfo(userId: Int) : SingleLiveEvent<User>{
        return createSimpleEvent {
            val result = mModel.fetchUserInfo(userId)
            result.data
        }
    }

    fun isFollowed(followerId: Int, userId: Int) : SingleLiveEvent<Boolean> {
        return createSimpleEvent {
            val result = mModel.isFollowed(followerId, userId)
            result.data
        }
    }

    fun unFollow(userId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.unFollow(MMKVHelper.getUser()!!.userId, userId)
            result.msg
        }
    }

    fun follow(userId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val followRec = FollowRec(frFollowerId = MMKVHelper.getUser()!!.userId, frUserId = userId)
            val result = mModel.follow(followRec)
            result.msg
        }
    }

    fun scanBorrow(bookId: Int): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.scanBorrow(bookId)
            result.msg
        }
    }

    fun updateUser(user: User): SingleLiveEvent<String> {
        return createSimpleEvent {
            val result = mModel.updateUser(user)
            result.msg
        }
    }

    fun uploadAvatar(file: File, userId: Int, fileType: Int): SingleLiveEvent<FileEntity> {
        return createSimpleEvent {
            val result = mModel.uploadAvatar(file, userId, fileType)
            result.data
        }
    }

    fun checkExpiringRecord(): SingleLiveEvent<List<BookBorRec>> {
        return createSimpleEvent {
            val result = mModel.checkExpiringRecord()
            result.data
        }
    }

}
